package base.exa02_dataTypeAndPrint;

/**
 * 基本数据类型之间的转换学习
 */
public class Demo4_BaseDataTypeConvert {

    /**
     * 首先使byte类型：
     * byte类型转整型short,int,long,和浮点型float,double
     * byte类型可以转成char类型，但是仅仅表示的是char字符，且属于强制转换
     * byte类型不能转boolean类型
     */
    byte aByte;
    /**
     * short类型能转整型int,long
     *
     */
    short aShort;
    int anInt;
    long aLong;
    float aFloat;
    double aDouble;
    char aChar;
    boolean aBoolean;


    public static void main(String[] args) {
        //byteToOther();
        //shortToOther();
        charToOther();
    }

    /**
     * 本方法用于学习byte转其他类型是否会发生精度损失
     */
    public static void byteToOther(){
        Demo4_BaseDataTypeConvert base = new Demo4_BaseDataTypeConvert();
        //byte转short,int,long
        base.aByte = 127;
        System.out.println("byte value: "+base.aByte);
        base.aShort = base.aByte;
        base.anInt = base.aByte;
        base.aLong = base.aByte;
        System.out.println("byte to short: "+base.aShort);
        System.out.println("byte to int: "+base.anInt);
        System.out.println("byte to long: "+base.aLong);
        //byte 转float,double
        base.aFloat = base.aByte;
        base.aDouble = base.aByte;
        System.out.println("byte to float: "+base.aFloat);
        System.out.println("byte to double: "+base.aDouble);
        System.out.println("short 127 ?== float 127.0: "+(base.aShort==base.aFloat));
        //byte 转char
        base.aByte = -128;
        base.aChar = (char) base.aByte;
        System.out.println("-128 byte to char: "+base.aChar);
    }

    /**
     * short类型转其他类型
     */
    public static void shortToOther(){
        Demo4_BaseDataTypeConvert base = new Demo4_BaseDataTypeConvert();
        base.aShort = Short.MAX_VALUE;
        System.out.println("short value is: "+base.aShort);
        //short转byte,int,long
        base.aByte = (byte) base.aShort;//强转，会有精度损失
        base.anInt = base.aShort;
        base.aLong = base.aShort;
        System.out.println("short to byte: "+base.aByte);
        System.out.println("short to int: "+base.anInt);
        System.out.println("short to long: "+base.aLong);

        //short转float,double
        base.aFloat = base.aShort;
        base.aDouble = base.aShort;
        System.out.println("short to float: "+base.aFloat);
        System.out.println("short to double: "+base.aDouble);

        //short转char
        base.aChar = (char) base.aShort;//强转，可能会有精度损失
        System.out.println("short to char： "+base.aChar);
    }

    /**
     * char类型转其他类型
     */
    public static void charToOther(){
        Demo4_BaseDataTypeConvert base = new Demo4_BaseDataTypeConvert();
        //char转int,short,long
        base.aChar='\uffff';
        System.out.println("char value: "+base.aChar);
        base.anInt = base.aChar;
        base.aShort = (short) base.aChar;//强转，可能发送精度损失
        base.aLong = base.aChar;
        System.out.println("char to int: "+base.anInt);
        System.out.println("char to short: "+base.aShort);
        System.out.println("char to long: "+base.aLong);
        //char 转float,double
        base.aFloat = base.aChar;
        base.aDouble = base.aChar;
        System.out.println("char to float: "+base.aFloat);
        System.out.println("char to double: "+base.aDouble);
    }
}
